<?php


namespace api\app\controller;
use api\app\model\UserModel;
use api\app\model\WalletModel;
use api\app\validate\Vildate;

use cmf\controller\BaseController;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\Request;
use think\Validate;

class WalletController
{
    public function addM(Request $request)
    {
        $user_id = $request->hello;
        $data = input('post.');
        $money = $data['money'];
        if (!$money) {
            return error('请输入金额');
        }
        $user = UserModel::find($user_id);
        if (!$user) {
            return error('账号不存在');
        }
        $after = $user['wallet'] + $money;
        $res=false;
        $log=false;
        // 启动事务
        Db::startTrans();
        try {
            $res = wallet_log($user_id, $money, $user['wallet'], $after, '充值',1);
            $log=UserModel::where('id', '=', $user_id)
                ->update(['wallet' => $after]);


        } catch (\Exception $e) {
            Db::rollback();
            return json(['status' => 'error', 'message' => $e->getMessage()], 500);

        }
        if($res&&$log){
            Db::commit();
            return success('成功');

        }
        return error('充值失败');

    }




    public function Record(Request $request)
    {
        $user_id=$request->hello;

        try {
            // 调用 common 函数查询流水记录
            $logs = record($user_id);
            return json(['status' => 'success', 'logs' => $logs]);
        } catch (\Exception $e) {
            return json(['status' => 'error', 'message' => $e->getMessage()], 500);
        }
    }
}